package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.view.View;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.DiscountMenuEntity;
import cn.mumzone.pos.model.SettleCalculatePriceEntity;

/**
 * Created by qiuzhenhuan on 2019/10/9.
 */

public class ADA_DiscountMenu extends CommonAdapter<DiscountMenuEntity> {


    public ADA_DiscountMenu(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_discount_menu;
    }

    @Override
    protected void convert(ViewHolder holder, final DiscountMenuEntity discountMenuEntity, int position) {

        holder.setImageResource(R.id.iv_icon, discountMenuEntity.getIcon());

        holder.setText(R.id.tv_title, discountMenuEntity.getTitle());

        holder.setTextColor(R.id.tv_use_count, discountMenuEntity.getUseCount() > 0 ?
                mContext.getResources().getColor(R.color.order_status_service) :
                mContext.getResources().getColor(R.color.content_text_color));
        if (discountMenuEntity.getSelectCount() <= 0) {
            holder.setVisible(R.id.tv_use_count, true);
            holder.setVisible(R.id.tv_select_count, false);
        } else {
            holder.setVisible(R.id.tv_use_count, false);
            holder.setVisible(R.id.tv_select_count, true);
        }

        switch (discountMenuEntity.getType()) {
            case DiscountMenuEntity.TYPE_TIME_CARD:
                holder.setText(R.id.tv_use_count, discountMenuEntity.getUseCount() > 0 ?
                        String.format("有%s张可用", discountMenuEntity.getUseCount()) : "暂无可用");

                holder.setText(R.id.tv_select_count, String.format("已选%s张", discountMenuEntity.getSelectCount()));
                break;
            case DiscountMenuEntity.TYPE_COUPON:
                holder.setText(R.id.tv_use_count, discountMenuEntity.getUseCount() > 0 ?
                        String.format("有%s张可用", discountMenuEntity.getUseCount()) : "暂无可用");

                holder.setText(R.id.tv_select_count, String.format("已选%s张", discountMenuEntity.getSelectCount()));
                break;
            case DiscountMenuEntity.TYPE_ACTIVITY:
                holder.setText(R.id.tv_use_count, discountMenuEntity.getUseCount() > 0 ?
                        String.format("有%s个可参与", discountMenuEntity.getUseCount()) : "暂无可参与");

                holder.setText(R.id.tv_select_count, String.format("已参与%s个活动", discountMenuEntity.getSelectCount()));
                break;
            case DiscountMenuEntity.TYPE_REDEEM:
                holder.setText(R.id.tv_use_count, discountMenuEntity.getUseCount() > 0 ?
                        "有可换购商品" : "暂无可换购商品");

                holder.setText(R.id.tv_select_count, String.format("已换购%s个商品", discountMenuEntity.getSelectCount()));
                break;
        }

        holder.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(discountMenuEntity.getType());
                }
            }
        });
    }

    public void updateUseCountByType(int type, int useCount) {
        for (DiscountMenuEntity entity : getDataList()) {
            if (type == entity.getType()) {
                entity.setUseCount(useCount);
                break;
            }
        }
    }

    public void updateSelectCountByType(int type, int selectCount) {
        for (DiscountMenuEntity entity : getDataList()) {
            if (type == entity.getType()) {
                entity.setSelectCount(selectCount);
                break;
            }
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int type);
    }
}
